//
//  LYTextAttachment.swift
//  EmoticonKeyboard
//
//  Created by liuerying on 16/6/1.
//  Copyright © 2016年 liuerying. All rights reserved.

// 自定义附件类

import UIKit

class LYTextAttachment: NSTextAttachment {

    //表情的中文
    var chs : String?
    
    //将表情图片添加到属性文本中
    class func emoticonImageToImageText(em:LYEmoticon,font:UIFont)->NSAttributedString{
    
        let attachment = LYTextAttachment()
        
        //给附件对象添加图片
        attachment.image = UIImage(contentsOfFile: em.imagePath ?? "")
        
        //设置文本
        attachment.chs = em.chs
        
        let lineHeight = font.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        //通过附件对象 实例化属性字符串
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        
        //给属性文本添加font属性 但是不可变的属性文本是不能添加属性的
        imageText.addAttributes([NSFontAttributeName : font], range: NSMakeRange(0,1) )
        
        return imageText
    }
}
